import { MeetingType, MeetingStatus, mockMeetings, Meeting } from '@/mock/data/meetingData'

// mock API 函数
export const mockAPI = {
  getMeetingList: async (params: any) => {
    // 列表查询延迟 1 秒
    await new Promise(resolve => setTimeout(resolve, 1000))
    let filteredMeetings = [...mockMeetings]
    
    if (params.type && params.type !== MeetingType.ALL) {
      filteredMeetings = filteredMeetings.filter(m => m.type === params.type)
    }
    if (params.status) {
      filteredMeetings = filteredMeetings.filter(m => m.status === params.status)
    }
    if (params.name) {
      filteredMeetings = filteredMeetings.filter(m => 
        m.name.toLowerCase().includes(params.name?.toLowerCase() || '')
      )
    }

    const start = (params.page - 1) * params.size
    const end = start + params.size
    const paginatedMeetings = filteredMeetings.slice(start, end)

    return {
      data: {
        current_data: paginatedMeetings,
        total_record: filteredMeetings.length
      }
    }
  },

  getMeetingDetail: async (id: number) => {
    // 详情查询延迟 800ms
    await new Promise(resolve => setTimeout(resolve, 800))
    const meeting = mockMeetings.find(m => m.id === id)
    if (!meeting) throw new Error('Meeting not found')
    return { data: meeting }
  },

  createMeeting: async (data: Partial<Meeting>) => {
    // 创建操作延迟 1.5 秒
    await new Promise(resolve => setTimeout(resolve, 1500))
    const newMeeting: Meeting = {
      id: Math.max(...mockMeetings.map(m => m.id)) + 1,
      ...data,
      browseCount: 0,
      goodCount: 0
    } as Meeting
    mockMeetings.push(newMeeting)
    return { data: newMeeting }
  },

  updateMeeting: async (id: number, data: Partial<Meeting>) => {
    // 更新操作延迟 1.2 秒
    await new Promise(resolve => setTimeout(resolve, 1200))
    const index = mockMeetings.findIndex(m => m.id === id)
    if (index === -1) throw new Error('Meeting not found')
    mockMeetings[index] = { ...mockMeetings[index], ...data }
    return { data: mockMeetings[index] }
  },

  deleteMeeting: async (id: number) => {
    // 删除操作延迟 1 秒
    await new Promise(resolve => setTimeout(resolve, 1000))
    const index = mockMeetings.findIndex(m => m.id === id)
    if (index === -1) throw new Error('Meeting not found')
    mockMeetings.splice(index, 1)
    return { data: null }
  },

  batchDeleteMeetings: async (ids: number[]) => {
    // 批量删除延迟 1.5 秒
    await new Promise(resolve => setTimeout(resolve, 1500))
    ids.forEach(id => {
      const index = mockMeetings.findIndex(m => m.id === id)
      if (index !== -1) mockMeetings.splice(index, 1)
    })
    return { data: null }
  },

  updateMeetingStatus: async (id: number, status: MeetingStatus) => {
    // 状态更新延迟 1 秒
    await new Promise(resolve => setTimeout(resolve, 1000))
    const meeting = mockMeetings.find(m => m.id === id)
    if (!meeting) throw new Error('Meeting not found')
    meeting.status = status
    return { data: meeting }
  }
} 